//
//  RJWarningAQICheckView.m
//  AirQuality
//
//  Created by Po on 15/4/14.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJWarningAQICheckView.h"
#import "RJWarningAQICheckDay.h"
#import "RJTools.h"

@interface RJWarningAQICheckView () <RJWarningFatherDeleaget, RJWarningAQICheckDayDelegate>

@property (retain, nonatomic) NSMutableArray * daysArray;

- (void)initInterface;

@end

@implementation RJWarningAQICheckView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initInterface];
        [self setFatherDelegate:self];
    }
    return self;
}

- (void)initInterface
{
    _daysArray = [[NSMutableArray alloc] init];
    NSUInteger dayCount = 1;
    for (NSUInteger i = 0; i < 3 ; i ++) {
        RJWarningAQICheckDay * day = [[RJWarningAQICheckDay alloc] initWithFrame:CGRectMake(0, 250 *  i, ScreenWidth, 240)];
        [day.timeLabel setText:[RJTools getTimeWithFormat:@"MM月dd日" time:GetFutureSinceNow(dayCount)]];
        [day setDelegate:self];
        [self addSubview:day];
        [_daysArray addObject:day];
        dayCount ++;
    }
}
#pragma mark - function
- (NSArray *)getDaysDetailWithData:(NSArray *)data
{
    NSMutableArray * daysDetailArray = [[NSMutableArray alloc] init];
    for (NSUInteger i = 0; i < 3; i ++) {
        NSMutableArray * array = [[NSMutableArray alloc] init];
        for (NSDictionary * dic in data) {
            NSString * aqiString = [NSString stringWithFormat:@"AQI范围预报 - %@",dic[@"AQI"][i]];
            [array addObject:@{dic[@"USERNAME"]:@[aqiString]}];
        }
        [daysDetailArray addObject:array];
    }
    return daysDetailArray;
}

#pragma mark - WarningFatherDelegate
- (void)WarningFatherSetData:(NSArray *)data
{
    if ([RJUserData defaultRJUserData].isDone || [RJUserData defaultRJUserData].isAudit) {
        NSArray * aqiRangeDays = [self getDaysDetailWithData:(data[0])];
        NSArray * expertAQI;
        NSArray * expertAQINPO;
        NSArray * expertAQIPO;
        NSArray * mainClassAQI;
        
        if ([RJUserData defaultRJUserData].isAudit) {
            expertAQI = [NSArray arrayWithArray:data[1]];
            expertAQINPO = [NSArray arrayWithArray:data[2]];
            mainClassAQI = [NSArray arrayWithArray:data[3]];
            expertAQIPO = [NSArray arrayWithArray:data[4]];
        }
        
        for (NSUInteger i = 0; i < 3; i ++) {
            RJWarningAQICheckDay * day = _daysArray[i];
            day.detailData = aqiRangeDays[i];
            
            if ([RJUserData defaultRJUserData].isAudit) {
                [day.AQICenterLabel setText:expertAQINPO[i]];
                [day.AQIRangeLabel setText:expertAQIPO[i]];
                [day.expertLabel setText:expertAQI[i]];
                [day.mainClassCheckLabel setText:mainClassAQI[i]];
                
                [day.mainClassLineLabel setHidden:YES];
                [day.ExpertLineLabel setHidden:YES];
                [day.AQIRangeLineLabel setHidden:YES];
                [day.AQIcenterTextField setHidden:YES];
                [day.AQImaxTextFiled setHidden:YES];
                [day.AQIminTextFiled setHidden:YES];
                [day.expertMaxTextField setHidden:YES];
                [day.expertMinTextField setHidden:YES];
                [day.mainClassCheckMaxTextField setHidden:YES];
                [day.mainClassCheckTextField setHidden:YES];
            }
        }
    }
}

- (void)WarningFatherSetUserData:(NSDictionary *)data
{
    NSMutableArray * aqiNpo = data[@"aqi_npo"];
    NSMutableArray * aqiAudit = data[@"aqi_audit"];
    NSMutableArray * aqi = data[@"aqi"];
    NSMutableArray * aqiRange = data[@"aqi_range"];
    for (NSUInteger i = 0; i < 3; i ++) {
        RJWarningAQICheckDay * day = _daysArray[i];
        if ([aqiNpo count] != 0) {
            [day.AQIcenterTextField setText:aqiNpo[i]];
        }
        if ([aqi count] != 0) {
            NSString * expertString = aqi[i];
            NSRange expertStringRange = [expertString rangeOfString:@"~"];
            [day.expertMinTextField setText:[expertString substringToIndex:expertStringRange.location]];
            [day.expertMaxTextField setText:[expertString substringFromIndex:expertStringRange.location + 1]];
        }
        if ([aqiAudit count] != 0) {
            NSString * mainClassString = aqiAudit[i];
            NSRange mainClassStringRange = [mainClassString rangeOfString:@"~"];
            [day.mainClassCheckTextField setText:[mainClassString substringToIndex:mainClassStringRange.location]];
            [day.mainClassCheckMaxTextField setText:[mainClassString substringFromIndex:mainClassStringRange.location + 1]];
        }
        if ([aqiRange count] != 0) {
            NSString * aqiRangeString = aqiRange[i];
            NSRange aqiRangeStingRange = [aqiRangeString rangeOfString:@"~"];
            [day.AQIminTextFiled setText:[aqiRangeString substringToIndex:aqiRangeStingRange.location]];
            [day.AQImaxTextFiled setText:[aqiRangeString substringFromIndex:aqiRangeStingRange.location + 1]];
        }
    }
}

- (void)WarningFatherWillRemoveSelf
{
}

- (NSDictionary *)WarningFatherGetData
{
    NSMutableArray * aqiNpo = [NSMutableArray array];
    NSMutableArray * aqiAudit = [NSMutableArray array];
    NSMutableArray * aqi = [NSMutableArray array];
    NSMutableArray * aqiRange = [NSMutableArray array];
    for (NSUInteger i = 0; i < 3; i ++) {
        RJWarningAQICheckDay * day = _daysArray[i];
        [aqiNpo addObject:day.AQIcenterTextField.text];
        [aqiAudit addObject:[NSString stringWithFormat:@"%@~%@",day.mainClassCheckTextField.text,day.mainClassCheckMaxTextField.text]];
        [aqi addObject:[NSString stringWithFormat:@"%@~%@",day.expertMinTextField.text,day.expertMaxTextField.text]];
        [aqiRange addObject:[NSString stringWithFormat:@"%@~%@",day.AQIminTextFiled.text,day.AQImaxTextFiled.text]];
    }
    return @{@"aqi_npo":aqiNpo,@"aqi_audit":aqiAudit,@"aqi":aqi,@"aqi_range":aqiRange};
}

#pragma mark - RJWarningAQICheckDelegate
- (void)WarningDay:(RJWarningAQICheckDay *)warningDay number:(CGFloat)number
{
    NSUInteger mathNum = [RJTools getAQIMathNumber:number];
    NSInteger minAQI = number - mathNum;
    minAQI = (minAQI < 0 ? 0 : minAQI);
    NSInteger maxAQI = number + mathNum;
    [warningDay.expertMaxTextField setText:GetStringWithInt(maxAQI)];
    [warningDay.expertMinTextField setText:GetStringWithInt(minAQI)];
}
@end
